<?php

$logFilesPath = __dir__ . '/../../Logs/';

echo 'Process archive log on ' . $logFilesPath . '<br />';

/**
* Creates a compressed zip file 
*/
function create_zip($files, $destination = '', $overwrite = false) 
{
	try
	{
		echo 'create_zip with ' . count($files) . ' files ' . '<br />';

		//if the zip file already exists and overwrite is false, return false
		if(file_exists($destination) && !$overwrite) 
		{ 
			echo 'Destination file was not found : ' . $destination . '<br />';
			return false; 
		}
		echo 'Log directory was found'. '<br />';
		
		//vars
		$valid_files = array();
		//if files were passed in...
		if(is_array($files)) 
		{
			echo 'files variable is array'. '<br />';
			//cycle through each file
			foreach($files as $file) {
				//make sure the file exists
				if(file_exists($file)) 
				{
					$valid_files[] = $file;
				}
				{
					$valid_files[] = $file;
					//echo 'file ' . $file . ' doesn\'t exist <br />';
				}
			}
		}
		
		echo count($valid_files) . ' files was validated' . '<br />';
		
		//if we have good files...
		if(count($valid_files)) 
		{
			//create the archive
			$zip = new ZipArchive();
			echo 'Zip file was initialized'. '<br />';
			if($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
				return false;
			}
			//add the files
			foreach($valid_files as $file) {
				echo 'Add file to zip ' . $file . '<br />';
				$zip->addFile($file, $file);
			}
			
			//debug
			echo 'The zip archive contains ' , $zip->numFiles, ' files with a status of ', $zip->status . '< br/>';
			
			echo 'The zip archive status ' . $zip->getStatusString(). '< br/>';
			
			//close the zip -- done!
			$zip->close();
			
			//check to make sure the file exists
			return file_exists($destination);
		}
		else
		{
			echo 'Zip file was initialized was not found'. '<br />';
			return false;
		}
	}
	catch(Exception $ex)
	{
		echo $ex->getMessage();
	}
}

/**
* Get all files to archives
*/
function getFilesToArchives($rootPath)
{
	$commons = $rootPath . 'CoreCommons/';
	$components = $rootPath . 'CoreComponents/';
	$datastorage = $rootPath . 'Datastorage/';
	
	$files = array();
	
	$commonsfiles = preg_grep('/^([^.])/', scandir($commons));
	$componentsfiles = preg_grep('/^([^.])/', scandir($components));
	$datastoragefiles = preg_grep('/^([^.])/', scandir($datastorage));
	
	foreach($commonsfiles as $file)
	{
		array_push($files, $commons . $file);
	}
	
	foreach($componentsfiles as $file)
	{
		array_push($files, $components . $file);
	}
	
	foreach($datastoragefiles as $file)
	{
		array_push($files, $datastorage . $file);
	}
	
	echo count($files) . ' files was found' . '<br />';
	
	return $files;
}

/*
//Create a zip with all logs files
$files_to_zip = array(
	'preload-images/1.jpg',
	'preload-images/2.jpg',
	'preload-images/5.jpg',
	'kwicks/ringo.gif',
	'rod.jpg',
	'reddit.gif'
);
*/

$files = getFilesToArchives($logFilesPath);

$result = create_zip($files, $logFilesPath . 'archive.zip', true);

if($result == true)
{
	//Delete log directory
	unlink($logFilesPath);
}

?>